package com.example.e_aop

/**
@author whd
@date 2023/11/25 15:33:41
 */


object AspectHandler {
  fun handle(instance: Any) {
    val clazz: Class<*> = instance.javaClass

    // 获取所有方法
    val methods = clazz.declaredMethods
    // 遍历所有方法
    for (method in methods) {
      // 检查方法上是否有 LogExecutionTime 注解
      if (method.isAnnotationPresent(LogExecutionTime::class.java)) {
        val startTime = System.nanoTime()
        // 调用带有注解的方法
        try {
          val invoke = method.invoke(instance)
        } catch (e: Exception) {
          e.printStackTrace()
        }
        val endTime = System.nanoTime()
        val executionTime = endTime - startTime
        println("Method " + method.name + " executed in " + executionTime + " nanoseconds.")
      }
    }
  }
}

